package net.huashitong.ssydt.provider.common.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.huashitong.ssydt.common.entity.vo.*;
import net.huashitong.ssydt.provider.common.service.CommonService;
import net.huashitong.ssydt.provider.qiniu.utils.QiniuUtils;
import net.huashitong.ssydt.provider.utils.date.DateUtils;
import net.huashitong.ssydt.provider.web.model.Result;
import net.huashitong.ssydt.provider.web.utils.ResultUtils;
import net.huashitong.ssydt.security.utils.UUIDGenerator;
import net.huashitong.ssydt.security.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 七牛相关信息
 *
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.08.14
 */
@Api(value = "/api/common/", tags = {"公共模块"})
@RestController
@RequestMapping("/api/common")
public class ApiCommonController {

    private CommonService commonService;

    private HttpServletRequest request;

    @Autowired
    public ApiCommonController(CommonService commonService, HttpServletRequest request) {
        this.commonService = commonService;
        this.request = request;
    }

    /**
     * 获取七牛上传凭证- simpleUpToken
     *
     * @param number
     * @return
     */
    @ApiOperation(value = "获取七牛上传凭证",
            notes = "获取七牛上传凭证",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/qiniu/simpleUpToken/{number}")
    public Result<HashMap<String, Object>> simpleUpToken(
            @ApiParam(value = "获取文件名数量", required = true, defaultValue = "1")
            @PathVariable Integer number) {
        String upToken = QiniuUtils.getSimpleUpToken();
        HashMap<String, Object> map = new HashMap<String, Object>();
        String baseUserUri = new StringBuilder().append("").toString();
        String relativeDirectory = new StringBuilder(DateUtils.getDate("yyyy/MM/dd/")).toString();
        List<String> list = new ArrayList<String>();
        map.put("upToken", upToken);
        for (int i = 0; i < number; i++) {
            String filename = System.currentTimeMillis() + UUIDGenerator.randomBase62(5);
            int length = baseUserUri.length() + relativeDirectory.length() + filename.length();
            StringBuilder fileName = new StringBuilder(length).append(baseUserUri).append(relativeDirectory).append(filename);
            list.add(fileName.toString());
        }
        map.put("fileNames", list);
        return ResultUtils.getSuccessResults(map);
    }

    @ApiOperation(value = "获取省份列表", notes = "获取省份列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/provinces")
    public Result<ArrayList<AreaVo>> provinceList() {
        List<AreaVo> list = commonService.provinceList();
        return ResultUtils.getSuccessResultList(list);
    }

    @ApiOperation(value = "获取地级市列表", notes = "获取地级市列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/province/{provinceId}/citys")
    public Result<ArrayList<AreaVo>> cityList(
            @ApiParam(value = "省份id", required = true)
            @PathVariable("provinceId") String provinceId) {
        List<AreaVo> list = commonService.cityList(provinceId);
        return ResultUtils.getSuccessResultList(list);
    }

    @ApiOperation(value = "获取县级列表", notes = "获取县级列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/province/city/{cityId}/countys")
    public Result<ArrayList<AreaVo>> countyList(
            @ApiParam(value = "城市id", required = true)
            @PathVariable("cityId") String cityId) {
        List<AreaVo> list = commonService.countyList(cityId);
        return ResultUtils.getSuccessResultList(list);
    }

    @ApiOperation(value = "地区列表（三级嵌套）", notes = "地区列表（三级嵌套）", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/areas")
    public Result<ArrayList<AreasVo>> areasList() {
        List<AreasVo> list = commonService.areasList();
        return ResultUtils.getSuccessResultList(list);
    }


    @ApiOperation(value = "banner列表", notes = "banner列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/banners")
    public Result<ArrayList<CommonBannerVO>> getBannerList() {
        List<CommonBannerVO> list = commonService.getBannerList();
        return ResultUtils.getSuccessResultList(list);
    }

    @ApiOperation(value = "提交问题反馈", notes = "提交问题反馈", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("/feedback")
    public Result<String> postFeedback(@RequestBody FeedbackVO feedbackVO) {
        String userId = UserUtils.getUserId();
        commonService.postFeedback(feedbackVO, userId);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "文章分享", notes = "文章分享", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/share/article/{articleId}")
    public Result<ShareOutVO> getShareArticle(@ApiParam(value = "文章id", required = true)
                                              @PathVariable("articleId") Long articleId) {
        String userId = UserUtils.getUserId();
        ShareOutVO shareOutVO = commonService.getShareArticle(articleId, userId, request);
        return ResultUtils.getSuccessResults(shareOutVO);
    }

    @ApiOperation(value = "题目分享", notes = "题目分享", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/share/question/{questionId}")
    public Result<ShareOutVO> getShareQuestion(@ApiParam(value = "题目id", required = true)
                                               @PathVariable("questionId") Long questionId) {
        String userId = UserUtils.getUserId();
        ShareOutVO shareOutVO = commonService.getShareQuestion(questionId, userId, request);
        return ResultUtils.getSuccessResults(shareOutVO);
    }

    @ApiOperation(value = "测评报告分享", notes = "测评报告分享", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/share/exam-report/type/{type}/recordId/{recordId}")
    public Result<ShareOutVO> getShareExamReport(@ApiParam(value = "类型 01全真刷题 02题库测评 03文章测评", required = true)
                                                 @PathVariable("type") String type,
                                                 @ApiParam(value = "记录id", required = true)
                                                 @PathVariable("recordId") Long recordId) {

        String userId = UserUtils.getUserId();
        ShareOutVO shareOutVO = commonService.getShareExamReport(type, recordId, userId, request);
        return ResultUtils.getSuccessResults(shareOutVO);
    }

    @ApiOperation(value = "获取公告", notes = "获取公告,如果没有公告，返回空Result", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/notice")
    public Result<NoticeVO> getNotice() {
        String userId = UserUtils.getUserId();
        NoticeVO noticeVO = commonService.getNotice(userId);
        return ResultUtils.getSuccessResults(noticeVO);
    }

    @ApiOperation(value = "获取我的2017", notes = "获取我的2017", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/year-score/{userId}")
    public Result<YearScoreVO> getYearScore(@PathVariable String userId) {
        YearScoreVO yearScore = commonService.getYearScore(userId);
        return ResultUtils.getSuccessResults(yearScore);
    }


}
